package com.abc.structure.binarySearchTree;


import com.abc.structure.binarySearchTree.file.Files;
import com.abc.structure.binarySearchTree.printer.BinaryTrees;

import java.util.Comparator;

public class BinarySearchTreeTest {

    public static void main(String[] args) {

        test8();
    }

    static void test8() {
        Integer data[] = new Integer[] {
                7, 4, 9, 2, 5, 8, 11, 3, 12, 1
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }

        BinaryTrees.println(bst);

        bst.remove(4);
        BinaryTrees.println(bst);

    }

    static void test1() {
        Integer[] data = new Integer[]{
                7, 4, 9, 2, 5, 1, 3, 8, 11, 12
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);
    }

    static void test2() {
        Integer[] data = new Integer[]{
                7, 4, 9, 2, 5, 1, 3, 8, 11, 12
        };

        BinarySearchTree<Person> bst = new BinarySearchTree<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        for (int i = 0; i < data.length; i++) {
            bst.add(new Person(data[i]));
        }

        BinaryTrees.println(bst);

    }

    static void test3() {
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < 40; i++) {
            bst.add((int)(Math.random() * 100));
        }
        String resultStr = BinaryTrees.printString(bst);
        resultStr += "\n\n";
        Files.writeToFile("E:\\binarySearchTree.txt", resultStr, true);
    }

    static void test4() {
        Integer[] data = new Integer[]{
                7, 4, 9, 2, 5, 1, 3, 8, 11, 10, 12
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);
//        bst.preorderTraversal();
//        bst.inorderTraversal();
//        bst.postorderTraversal();
//        bst.levelOrderTraversal();
//        bst.preorder(new BinarySearchTree.Visitor<Integer>() {
//            @Override
//            public void visit(Integer element) {
//                System.out.println(element);
//            }
//        });
        bst.inorder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public void visit(Integer element) {
                System.out.println(element);
                if (element.equals(5)) {
                    stop = true;
                }
            }
        });
//        bst.postorder(new BinarySearchTree.Visitor<Integer>() {
//            @Override
//            public void visit(Integer element) {
//                System.out.println(element);
//                if (element.equals(5)) {
//                    stop = true;
//                }
//            }
//        });
    }

    static void test5() {
        Integer[] data = new Integer[]{
                7, 4, 9, 2, 5, 1, 3, 8, 11, 10, 12
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);

        System.out.println(bst.toString());
    }

    static void test6() {
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < 20; i++) {
            bst.add((int)(Math.random() * 100));
        }
        BinaryTrees.println(bst);

        System.out.println(bst.height());
    }

    static void test7() {
        Integer[] data = new Integer[]{
                39, 6, 50, 0, 14, 49, 52, 42
        };

        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);
        System.out.println(bst.isComplete());
    }
}
